using System;
using System.Data;
using System.Collections.Generic;
using AIM.Common;
using AIM.Model;
using BrightFrame.Aop;
namespace AIM.BLL
{
    [Connection]
	public class RoleBLL : ContextBoundObject
	{
		private readonly AIM.DAL.RoleDAL _roleDAL = new AIM.DAL.RoleDAL();
        private readonly AIM.DAL.RoleHasPurviewDAL _roleHasPurviewDAL = new DAL.RoleHasPurviewDAL();
        private readonly AIM.DAL.SystemDAL _systemDAL = new DAL.SystemDAL();

        public Role GetModel(int roleID)
        {
            return _roleDAL.GetModel(roleID);
        }

        [TransactionMethod]
        public void Add(Role role, List<RoleHasPurview> roleHasPurviewList)
        {
            _roleDAL.Add(role);
            role.RoleID = _systemDAL.GetLastInsertID();
            foreach (RoleHasPurview roleHasPurview in roleHasPurviewList)
            {
                roleHasPurview.Role = role;
                _roleHasPurviewDAL.Add(roleHasPurview);
            }
        }

        [TransactionMethod]
        public void Update(Role role, List<RoleHasPurview> roleHasPurviewList)
        {
            _roleDAL.Update(role);
            _roleHasPurviewDAL.DeleteByRoleID(role.RoleID);
            foreach (RoleHasPurview roleHasPurview in roleHasPurviewList)
            {
                _roleHasPurviewDAL.Add(roleHasPurview);
            }
        }

        [TransactionMethod]
        public void Delete(int roleID)
        {
            _roleHasPurviewDAL.DeleteByRoleID(roleID);
            _roleDAL.Delete(roleID);
        }

        public DataTable QueryAll()
        {
            return _roleDAL.QueryAll();
        }
	}
}

